package com.i98labs.gaelyk.frontcontroller.util

class UrlUtil {
	
	static getController = { url ->
		if (url) {
			def tokens = url?.split("/")
			return tokens.length > 0 ? tokens[0] : "index"
		}
		return "index"
	}
	
	static getAction = { url ->
		if (url) {
			def tokens = url?.split("/")
			return tokens.length > 1 ? tokens[1] : "index"
		}
		return "index"
	}
	
	static getId = { url ->
		if (url) {
			def tokens = url?.split("/")
			return tokens.length > 2 && tokens[2].isLong() ? tokens[2] : null 
		}
		return null
	}
	
	static getParams = { url -> 
		def parameters = new LinkedHashMap()
		if (url) {
			def tokens = url?.split("/")
			if (tokens.length > 2) {
				for (int i = 2 + (UrlUtil.getId(url) ? 1:0); i < tokens.length - 1; i += 2) {
					parameters.put( tokens[i], tokens[i+1] ) 
				}
			}
		}
		parameters
	}
}
